#!/usr/bin/env bash

set -e -u

if [ -z "${API_UMBRELLA_ROOT:-}" ]; then
  export API_UMBRELLA_ROOT="/opt/api-umbrella"
fi

if [ -z "${API_UMBRELLA_EMBEDDED_ROOT:-}" ]; then
  export API_UMBRELLA_EMBEDDED_ROOT="$API_UMBRELLA_ROOT/embedded"
fi

if [ -z "${API_UMBRELLA_SRC_ROOT:-}" ]; then
  export API_UMBRELLA_SRC_ROOT="${API_UMBRELLA_EMBEDDED_ROOT}/app"
fi

if [ -z "${API_UMBRELLA_RUNTIME_CONFIG:-}" ]; then
  export API_UMBRELLA_RUNTIME_CONFIG="${API_UMBRELLA_ROOT}/var/run/runtime_config.yml"
fi

# Set the path for binaries.
export PATH=$API_UMBRELLA_EMBEDDED_ROOT/sbin:$API_UMBRELLA_EMBEDDED_ROOT/bin:$PATH

# Set the Lua load paths to include the vendored app dependencies and the
# embedded openresty installation.
#
# Note that we purposefully don't use any of the default Lua load paths (like
# /usr/local) so that other Lua packages on the system aren't picked up (since
# they might conflict).
export LUA_PATH="$API_UMBRELLA_EMBEDDED_ROOT/app/vendor/share/lua/5.1/?.lua;\
$API_UMBRELLA_EMBEDDED_ROOT/app/vendor/share/lua/5.1/?/init.lua;\
$API_UMBRELLA_EMBEDDED_ROOT/openresty/lualib/?.lua;\
$API_UMBRELLA_EMBEDDED_ROOT/openresty/lualib/?/init.lua;\
$API_UMBRELLA_EMBEDDED_ROOT/openresty/luajit/share/luajit-2.1.0-beta2/?.lua;\
$API_UMBRELLA_EMBEDDED_ROOT/openresty/luajit/share/luajit-2.1.0-beta2/?/init.lua;\
$API_UMBRELLA_EMBEDDED_ROOT/openresty/luajit/share/lua/5.1/?.lua;\
$API_UMBRELLA_EMBEDDED_ROOT/openresty/luajit/share/lua/5.1/?/init.lua"
if [ -n "$API_UMBRELLA_SRC_ROOT" ]; then
  export LUA_PATH="$API_UMBRELLA_SRC_ROOT/src/?.lua;$LUA_PATH"
fi
export LUA_CPATH="$API_UMBRELLA_EMBEDDED_ROOT/app/vendor/lib/lua/5.1/?.so;\
$API_UMBRELLA_EMBEDDED_ROOT/openresty/lualib/?.so;\
$API_UMBRELLA_EMBEDDED_ROOT/openresty/luajit/lib/lua/5.1/?.so"

resty --http-include "import.conf" import.lua
